
//
//  UIImage+Extension.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/25.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit


extension UIImage {
    
    ///  截屏
    class func getScreenImage() -> UIImage {
        
        let window = UIApplication.sharedApplication().keyWindow!
        //第三个参数为0 表示取当前设备界面的比例
        UIGraphicsBeginImageContextWithOptions(window.bounds.size, true, 0)
        //将屏幕内容拷贝进入上下文
        window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: true)
        let resImg = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        return resImg
    }
    
    func thinnerPicTo(width width:CGFloat) -> UIImage {
        
        if width >= self.size.width {
            return self  //目标尺寸大于本身
        } else {
            
            let newHeight = width / self.size.width * self.size.height
            
            let size = CGSize (width: width, height: newHeight)
            let rect = CGRect(x: 0, y: 0, width: width, height: newHeight)
            
            UIGraphicsBeginImageContextWithOptions(size, true, 0)
            //绘制
            drawInRect(rect)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            
            UIGraphicsEndImageContext()
            return image
        }
    }
}
